﻿Imports System.IO
Imports MessageBoxEx

Public Class frmRestore
    Public backupFiles As String()

    Private Sub frmRestore_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

        With listviewBackups

            .View = View.Details

            .Columns.Add("Creation Date/Time")
            .Columns.Add("Demonbuddy Version")
            .Columns.Add("Settings Available")
            .Columns.Add("Directory Path")

        End With

        Restore_Refresh()
    End Sub

    Public Sub Restore_Refresh()

        Dim backupCount As Integer
        Dim backupCur As Integer
        Dim listviewData(3) As String

        backupFiles = DBMA_GetBackupPaths()

        backupCount = UBound(backupFiles)

        listviewBackups.Items.Clear()

        For backupCur = 0 To backupCount

            listviewData(0) = Directory.GetCreationTime(Path.GetDirectoryName(backupFiles(backupCur)))
            listviewData(1) = File_GetVersionInfo(backupFiles(backupCur))

            If Directory.Exists(Path.GetDirectoryName(backupFiles(backupCur)) & "\Settings") = True Then
                listviewData(2) = "True"
            Else
                listviewData(2) = "False"
            End If

            listviewData(3) = Path.GetDirectoryName(backupFiles(backupCur))

            Dim listviewItem As ListViewItem = New ListViewItem(listviewData)

            listviewBackups.Items.Add(listviewItem)

        Next

        Form_ListView_AutoResize(listviewBackups)
    End Sub

    Private Function DBMA_GetBackupPaths() As String()

        Dim backupDirectory As String = My.Settings.FolderBackup
        Dim fileList As String()

        If Directory.Exists(backupDirectory) = False Then Return Nothing

        fileList = Directory.GetFiles(backupDirectory, "Demonbuddy.exe", SearchOption.AllDirectories)

        Return fileList
    End Function

    Private Sub cmdRestore_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdRestore.Click

        Dim boolBackup As Boolean

        If (listviewBackups.SelectedItems.Count = 0) Then

            Dim _Msg = New MsgBoxEx("No backup selected.", Me.Text, , MsgBoxExImage.Exclamation)
            _Msg.SetButtons(New String() {"OK"}, New DialogResult() {DialogResult.Yes}, 1)
            _Msg.ShowDialog(Me)

            Exit Sub

        End If

        If chkSettings.Checked = True Then

            If Directory.Exists(My.Settings.FolderDemonbuddy & "\Settings") Then

                Directory.Delete(My.Settings.FolderDemonbuddy & "\Settings", True)

            End If

            If _
                Directory.Exists(Path.GetDirectoryName(backupFiles(listviewBackups.FocusedItem.Index)) & "\Settings") =
                False Then

                Dim _Msg = New MsgBoxEx("No Settings directory found for this backup. Please select another", Me.Text, ,
                                        MsgBoxExImage.Exclamation)
                _Msg.SetButtons(New String() {"OK"}, New DialogResult() {DialogResult.Yes}, 1)
                _Msg.ShowDialog(Me)
                Exit Sub

            End If
            My.Computer.FileSystem.CopyDirectory(
                Path.GetDirectoryName(backupFiles(listviewBackups.FocusedItem.Index)) & "\Settings",
                My.Settings.FolderDemonbuddy & "\Settings")

            boolBackup = True

        Else

            boolBackup = Restore_Folder(Path.GetDirectoryName(backupFiles(listviewBackups.FocusedItem.Index)),
                                        My.Settings.FolderDemonbuddy, False, True)

        End If

        If boolBackup = True Then
            Dim _Msg = New MsgBoxEx("Restore successful.", Me.Text, , MsgBoxExImage.Check)
            _Msg.SetButtons(New String() {"OK"}, New DialogResult() {DialogResult.Yes}, 1)
            _Msg.ShowDialog(Me)
        Else
            Dim _Msg = New MsgBoxEx("Restore failed.", Me.Text, , MsgBoxExImage.Exclamation)
            _Msg.SetButtons(New String() {"OK"}, New DialogResult() {DialogResult.Yes}, 1)
            _Msg.ShowDialog(Me)
        End If

        Hide()
    End Sub

    Private Sub listviewBackups_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) _
        Handles listviewBackups.SelectedIndexChanged
    End Sub
End Class